/*
 * Amkai Enterprise Solution (AES.) 
 * Copyright 2004-2008 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler.billing
 * File: ClaimCustomizationHandler.java
 * Created: 2009.03.20. 11:13:50
 * Author: polaj 
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler.billing;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amkai.aes.aescommon.exceptions.AOException;
import com.amkai.aes.aescommon.manager.DepartmentManager;
import com.amkai.aes.aescommon.service.binary.job.billing.ClaimCustomizationJob;
import com.amkai.aes.aescommon.service.result.billing.ClaimCustomizationResult;
import com.amkai.aes.generatedbeans.bill.HCFA1500Customization;
import com.amkai.aes.generatedbeans.bill.UB04Customization;
import com.amkai.aes.generatedbeans.organization.Department;
import com.amkai.aes.server.manager.ClaimCustomizationManager;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;
import com.amkai.rad.designerbase.beans.ChangedKind;

public class ClaimCustomizationHandler implements JobObjectHandler<ClaimCustomizationJob, Department, ClaimCustomizationResult>{

	protected static final Log log = LogFactory.getLog(ClaimCustomizationHandler.class);
	protected static final String TITLE = "Claim Customization";
	
	@Override
	public ClaimCustomizationResult process(ClaimCustomizationJob job, Department dep) throws Exception {
		switch (job.getKind()) {
		case GET_HCFA1500_CUSTOMIZATION_OF_DEPARTMENT:	return getHCFA1500CustomizationOfDepartment(job,dep);
		case SAVE_HCFA1500_CUSTOMIZATION_OF_DEPARTMENT:	return saveHCFA1500CustomizationOfDepartment(job,dep);
		case GET_UB04_CUSTOMIZATION_OF_DEPARTMENT:	return getUB04CustomizationOfDepartment(job,dep);
		case SAVE_UB04_CUSTOMIZATION_OF_DEPARTMENT:	return saveUB04CustomizationOfDepartment(job,dep);
		case RESET_HCFA1500_CUSTOMIZATION:	return resetHCFA1500Customization(job, dep);
		case RESET_UB04_CUSTOMIZATION:	return resetUB04Customization(job, dep);
//		case GET_HCFA1500_INHERITANCE_SETTINGS:	return getHCFA1500InheritanceSettings(job,dep);
		default:	throw new AOException(TITLE,"Unknown Kind",job.getKind().toString(),null);
		}
	}
	
	protected ClaimCustomizationResult getHCFA1500CustomizationOfDepartment(ClaimCustomizationJob job, Department dep) throws Exception {
		ClaimCustomizationResult result = new ClaimCustomizationResult();
		SessionManager sm = SiteManager.Start();
		try {
			// KKORNEL: AES-14740
			Department department = sm.get(Department.class, dep.getId());
			result.setHcfa1500Customization(ClaimCustomizationManager.getHCFA1500CustomizationOfDepartment(sm, department, false));
			if (DepartmentManager.isSHLike(department)) {
				result.setHcfa1500CustomizationInpatient(ClaimCustomizationManager.getHCFA1500CustomizationOfDepartment(sm, department, true));
			}
		}
		catch (Exception ex) {
			log.error(TITLE, ex);
			result.setException(ex);
		}
		return result;
	}
	
	protected ClaimCustomizationResult saveHCFA1500CustomizationOfDepartment(ClaimCustomizationJob job, Department dep) throws Exception {
		ClaimCustomizationResult result = new ClaimCustomizationResult();
		SessionManager sm = SiteManager.Start();
		try {
			sm.openTransaction();

			HCFA1500Customization customization = job.getHcfa1500Customization(); 
			// KKORNEL :AES-14740
			HCFA1500Customization customizationInpatient = job.getHcfa1500CustomizationInpatient();
			Department department = sm.get(Department.class, dep.getId());
			boolean isnew = (customization.getChangedKind() == ChangedKind.CREATED);
			boolean isnewInpatient = (customizationInpatient != null) && (customizationInpatient.getChangedKind() == ChangedKind.CREATED);
			ClaimCustomizationManager.saveHCFA1500Customization(sm, customization);
			if (customizationInpatient != null) {
				ClaimCustomizationManager.saveHCFA1500Customization(sm, customizationInpatient);
			}
			if (isnew) {
				department.setHcfa1500Customization(customization);
			}
			if (isnewInpatient) {
				department.setHcfa1500CustomizationInpatient(customizationInpatient);
			}
			if (isnew || isnewInpatient) {
				sm.saveRAD(department);
			}
//			result.setHcfa1500Customization(customization);
			sm.commitTransaction();
			sm.clear();
			result.setHcfa1500Customization(ClaimCustomizationManager.getHCFA1500CustomizationOfDepartment(sm, dep, false));
			if (customizationInpatient != null) {
				result.setHcfa1500CustomizationInpatient(ClaimCustomizationManager.getHCFA1500CustomizationOfDepartment(sm, dep, true));
			}
			
		}
		catch (Exception ex) {
			sm.rollbackTransaction();
			log.error(TITLE, ex);
			result.setException(ex);
		}
		return result;
	}

	protected ClaimCustomizationResult getUB04CustomizationOfDepartment(ClaimCustomizationJob job, Department dep) throws Exception {
		ClaimCustomizationResult result = new ClaimCustomizationResult();
		SessionManager sm = SiteManager.Start();
		try {
			// KKORNEL: AES-14740
			Department department = sm.get(Department.class, dep.getId());
			result.setUb04Customization(ClaimCustomizationManager.getUB04CustomizationOfDepartment(sm, department, false));
			if (DepartmentManager.isSHLike(department)) {
				result.setUb04CustomizationInpatient(ClaimCustomizationManager.getUB04CustomizationOfDepartment(sm, department, true));
			}
		}
		catch (Exception ex) {
			log.error(TITLE, ex);
			result.setException(ex);
		}
		return result;
	}

	protected ClaimCustomizationResult saveUB04CustomizationOfDepartment(ClaimCustomizationJob job, Department dep) throws Exception {
		ClaimCustomizationResult result = new ClaimCustomizationResult();
		SessionManager sm = SiteManager.Start();
		try {
			sm.openTransaction();

			UB04Customization customization = job.getUb04Customization();
			// KKORNEL: AES-14740
			UB04Customization customizationInpatient = job.getUb04CustomizationInpatient();
			Department department = sm.get(Department.class, dep.getId());
			boolean isnew = (customization.getChangedKind() == ChangedKind.CREATED);
			boolean isnewInpatient = (customizationInpatient != null) && (customizationInpatient.getChangedKind() == ChangedKind.CREATED);
			ClaimCustomizationManager.saveUB04Customization(sm, customization);
			if (customizationInpatient != null) {
				ClaimCustomizationManager.saveUB04Customization(sm, customizationInpatient);
			}
			if (isnew) {
				department.setUb04Customization(customization);
			}
			if (isnewInpatient) {
				department.setUb04CustomizationInpatient(customizationInpatient);
			}
			if (isnew || isnewInpatient) {
				sm.saveRAD(department);
			}
			result.setUb04Customization(customization);
			result.setUb04CustomizationInpatient(customizationInpatient);
			sm.commitTransaction();
		}
		catch (Exception ex) {
			sm.rollbackTransaction();
			log.error(TITLE, ex);
			result.setException(ex);
		}
		return result;
	}
/*	
	protected ClaimCustomizationResult getHCFA1500InheritanceSettings(ClaimCustomizationJob job, Department moduleDepartment) throws Exception {
		ClaimCustomizationResult result = new ClaimCustomizationResult();
		SessionManager sm = SiteManager.Start();
		try {
			result.setHcfa1500Inheritance(ClaimCustomizationManager.getHCFA1500Inheritance(sm, job.getHcfa1500Customization()));
		}
		catch (Exception ex) {
			logger.error(EXCEPTION_TITLE, ex);
			result.setException(ex);
		}
		return result;
	}
	*/
	
	protected ClaimCustomizationResult resetHCFA1500Customization(ClaimCustomizationJob job, Department moduleDepartment) throws Exception {
		ClaimCustomizationResult result = new ClaimCustomizationResult();
		SessionManager sm = SiteManager.Start();
		try {
			sm.openTransaction();
			HCFA1500Customization customization = ClaimCustomizationManager.createDefaultHCFA1500Customization(sm);
			if (job.getResetID() != null) {
				customization.setId(job.getResetID());
				customization.setChangedKind(ChangedKind.CHANGED);
				ClaimCustomizationManager.saveHCFA1500Customization(sm, customization);
			}
			result.setHcfa1500Customization(customization);
			sm.commitTransaction();
		}
		catch (Exception ex) {
			sm.rollbackTransaction();
			result.setException(ex);
			log.error(TITLE, ex);
		}
		finally {
			sm.closeSession();
		}
		return result;
	}
	
	protected ClaimCustomizationResult resetUB04Customization(ClaimCustomizationJob job, Department moduleDepartment) throws Exception {
		ClaimCustomizationResult result = new ClaimCustomizationResult();
		SessionManager sm = SiteManager.Start();
		try {
			sm.openTransaction();
			UB04Customization customization = ClaimCustomizationManager.createDefaultUB04Customization(sm);
			if (job.getResetID() != null) {
				customization.setId(job.getResetID());
				customization.setChangedKind(ChangedKind.CHANGED);
				ClaimCustomizationManager.saveUB04Customization(sm, customization);
			}
			result.setUb04Customization(customization);
			sm.commitTransaction();
		}
		catch (Exception ex) {
			sm.rollbackTransaction();
			result.setException(ex);
			log.error(TITLE, ex);
		}
		finally {
			sm.closeSession();
		}
		return result;
	}
}
